/* 
Set directory 
*/

    global root ENTER DIRECTORY
                                
	global r4yc 		$root\Data\Young Lives\R4\UKDA-7931-stata11\stata11\india_r4\yc_chcog_anon
	global r4oc 		$root\Data\Young Lives\R4\UKDA-7931-stata11\stata11\india_r4\oc_chcog_anon
	global r3yc 		$root\Data\Young Lives\R3\survey_data_r3\india_r3\youngerchild
	global r3oc 		$root\Data\Young Lives\R3\survey_data_r3\india_r3\olderchild
	global r2yc	 		$root\Data\Young Lives\R2\survey_data_r2\india_r2\Younger Child
	global r2oc	 		$root\Data\Young Lives\R2\survey_data_r2\india_r2\Older Child
	global r1 			$root\Data\Young Lives\R1\survey_data_r1\india_r1
	global data 		$root\Dta\Young Lives
	global school 		$root\Data\Young Lives\School_2010-11\stata11
	global temp			$root\Data\Temperature\Derived


/* 
STEP TWO
Wave 2
*/

use "$r2yc\inchildlevel5yrold.dta", clear

*Creating Cohort Level Identifier
gen cohort = 0

gen age = 5

append using "$r2oc\inchildquest12yrold.dta" 

replace cohort = 1 if cohort == .

replace age = 12 if age == .

keep CHILDID STRTHRPP STRTMNPP FINHRPP FINMNPP STRTHRWM STRTMRWM FINHRRWM FINMNRWM

rename *, lower

*Creating Wave Identifiers
gen round = 2


save "$data\Round 2\round_2_time.dta", replace

/* 
STEP FOUR
Wave 3
*/

use "$r3yc\in_yc_childlevel.dta", clear

*Creating Cohort Level Identifier
gen cohort = 0

gen age = 8

*merge 1:1 CHILDID using "$data\Round 3\temp_in_yc_hhchild.dta"

append using "$r3oc\in_oc_childlevel.dta"

replace cohort = 1 if cohort == .

replace age = 15 if age == .

*Note: rppvt, rmath and rcloze = rppvt_co, rmath_co and rcloze_co

keep CHILDID STRTHRPP STRTMNPP FINHRPP FINMNPP  CNDSTHR CNDSTMN CNDENHR CNDENMN CMPSTHR CMPSTMN CMPENHR CMPENMN

rename *, lower

*Creating Wave Identifiers
gen round = 3


save "$data\Round 3\round_3_time.dta", replace

/* 
Round 4 - Wave 5
*/
use "$r4yc\in_r4_yccog_youngerchild.dta", clear
gen cohort = 0

append using "$r4oc\in_r4_occog_olderchild.dta", force

replace cohort = 1 if cohort == .

rename *, lower

gen tempin = "IN"
gen tempino = "IN0"
egen childid_1 = concat(tempin childcode)
egen childid_2 = concat(tempino childcode)
gen childid = childid_1
replace childid = childid_2 if childcode<99999

gen round = 5

keep strthrpp strtmnpp endhrpp endmnpp strthrmt strtmnmt endhrmt endmnmt childid round

save "$data\round_5_time.dta",replace

append using "$data\Round 3\round_3_time.dta"

append using "$data\Round 2\round_2_time.dta"

save "$data\time_survey_rounds.dta", replace

/* 
STEP FIVE
Wave 4: School Survey
*/

use "$school\z_in_sch_tblchildmathtestdh.dta", clear
	ren CHILDID-MTHRSP21, lower
keep childid  teststr1 testend1 teststr2 testend2
	gen round = 4 
	gen cohort = 0
	gen age = 10


keep childid round teststr1 testend1 teststr2 testend2

save "$data\School Survey\ss_time_math.dta",replace



/* 
STEP SIX
Appending all waves and creating variables for analysis
*/

use "$data\time_survey_rounds.dta", clear

append using "$data\School Survey\ss_time_math.dta"

sort childid round 

*Time and Duration of Test*
*PPVT
gen ppvt2_st_hr = strthrpp if round == 2
gen ppvt2_dur_temp1 =  finhrpp - strthrpp
gen ppvt2_dur_temp2 =  finmnpp - strtmnpp 
gen ppvt2_dur = ppvt2_dur_temp2 if ppvt2_dur_temp1 == 0
gen ppvt2_dur_temp3 = ppvt2_dur_temp1*60
replace ppvt2_dur = ppvt2_dur_temp3 + ppvt2_dur_temp2 if ppvt2_dur == .

*Math Round 2
gen math2_st_hr = strthrwm
gen math2_dur_temp1 =  finhrrwm - strthrwm
gen math2_dur_temp2 =  finmnrwm - strtmrwm
gen math2_dur = math2_dur_temp2 if math2_dur_temp1 == 0
gen math2_dur_temp3 = math2_dur_temp1*60
replace math2_dur = math2_dur_temp3 + math2_dur_temp2 if math2_dur == .

*Math Round 3
**Start Hour is computing time, duration is calculated using achievement test time 
gen math3_st_hr = cmpsthr
gen math3_dur_temp1 =  cmpenhr - cndenhr
gen math3_dur_temp2 =  cmpenmn - cndenmn
gen math3_dur = math3_dur_temp2 if math3_dur_temp1 == 0
gen math3_dur_temp3 = math3_dur_temp1*60
replace math3_dur = math3_dur_temp3 + math3_dur_temp2 if math3_dur == .

*Math Round 4
gen math4_st_hr = hhC(teststr1)
gen sthr = hhC(teststr1)
gen stmn = mmC(teststr1)
gen enhr = hhC(testend2)
gen enmn = mmC(testend2)

replace enhr = 24 if enhr == 0
replace sthr = 24 if sthr == 0
gen math4_dur_temp1 =  enhr - sthr 
gen math4_dur_temp2 =  enmn - stmn
gen math4_dur = math4_dur_temp2 if math4_dur_temp1 == 0
gen math4_dur_temp3 = math4_dur_temp1*60
replace math4_dur = math4_dur_temp3 + math4_dur_temp2 if math4_dur == .

*PPVT Round 5
gen ppvt5_st_hr = strthrpp if round == 5
gen ppvt5_dur_temp1 =  endhrpp - strthrpp if round == 5
gen ppvt5_dur_temp2 =  endmnpp - strtmnpp if round == 5
gen ppvt5_dur = ppvt5_dur_temp2 if ppvt5_dur_temp1 == 0 & round == 5
gen ppvt5_dur_temp3 = ppvt5_dur_temp1*60 if round == 5
replace ppvt5_dur = ppvt5_dur_temp3 + ppvt5_dur_temp2 if ppvt5_dur == . & round == 5

*Math Round 5
gen math5_st_hr = strthrmt if round == 5
gen math5_dur_temp1 =  endhrmt - strthrmt if round == 5
gen math5_dur_temp2 =  endmnmt - strtmnmt if round == 5
gen math5_dur = math5_dur_temp2 if math5_dur_temp1 == 0 & round == 5
gen math5_dur_temp3 = math5_dur_temp1*60 if round == 5
replace math5_dur = math5_dur_temp3 + math5_dur_temp2 if math5_dur == . & round == 5


egen math_st_hr = rowtotal(math2_st_hr math3_st_hr math4_st_hr math5_st_hr), missing
egen math_dur = rowtotal(math2_dur math3_dur math4_dur math5_dur), missing

egen ppvt_st_hr = rowtotal(ppvt2_st_hr ppvt5_st_hr), missing
egen ppvt_dur = rowtotal(ppvt2_dur ppvt5_dur), missing

*Start Hour: Cleaning: 88
replace math_st_hr = 0 if math_st_hr == 88
replace ppvt_st_hr = 0 if ppvt_st_hr == 77

*Duration: Cleaning
foreach var of varlist math_dur ppvt_dur {
replace `var' = . if `var' < 0
replace `var' = . if `var' > 90
replace `var' = . if `var' == 0
}

foreach var of varlist math_dur ppvt_dur {
winsor `var', gen(w_`var') p(0.01)
}

keep childid round w_math_dur w_ppvt_dur math_st_hr ppvt_st_hr

sort childid round 

order childid round 

save "$data\all_time.dta",replace

